package com.panghu.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.panghu.entity.ApiResult;
import com.panghu.entity.Teacher;
import com.panghu.service.TeacherService;
import com.panghu.service.impl.TeacherServiceImpl;
import com.panghu.utils.ApiResultHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

/**
 * <p>
 * 教师信息表 前端控制器
 * </p>
 *
 * @author huahua
 * @since 2022-03-19
 */
@RestController
public class TeacherController {

    @Autowired
    private TeacherService teacherService;


    @GetMapping("/teachers/{page}/{size}")
    public ApiResult findAll(@PathVariable Integer page, @PathVariable Integer size){
        //分页查询所有的教师信息
        Page<Teacher> utilP = new Page(page,size);
        Page<Teacher> result = teacherService.page(utilP, null);
        return ApiResultHandler.success(result);
    }

    @GetMapping("/teacher/{teacherId}")
    public ApiResult findById(@PathVariable("teacherId") Integer teacherId){
        //按id进行教师查找
        Teacher tea = teacherService.getById(teacherId);
        return ApiResultHandler.success(tea);
    }

    @DeleteMapping("/teacher/{teacherId}")
    public ApiResult deleteById(@PathVariable("teacherId") Integer teacherId){
        //按id进行教师删除
        boolean res = teacherService.removeById(teacherId);
        return ApiResultHandler.success(res);
    }

    @PutMapping("/teacher")
    public ApiResult update(@RequestBody Teacher teacher){
        //更新教师信息
        boolean res = teacherService.saveOrUpdate(teacher);
        return ApiResultHandler.success(res);
    }

    @PostMapping("/teacher")
    public ApiResult add(@RequestBody Teacher teacher){
        //添加教师
        boolean res = teacherService.save(teacher);
        return ApiResultHandler.success(res);
    }
}

